//
//  ManagerViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/21.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ManagerViewController.h"

//添加新地址
#import "ReadLoginData.h"
#import "GPDateView.h"
#import "NetWorkTools.h"

@interface ManagerViewController ()<UIAlertViewDelegate>;




@property (nonatomic, strong)UITableView *managertableview;
@property (nonnull, copy) NSString *str;
@property (strong, nonatomic)  UILabel *provinceLabel;
@property (strong, nonatomic)  UILabel *cityLabel;
@property (strong, nonatomic)  UILabel *areaLabel;

@property (weak, nonatomic) IBOutlet UILabel *city;

@property (weak, nonatomic) IBOutlet UILabel *prLL;

@property (weak, nonatomic) IBOutlet UILabel *areaCC;
@property (weak, nonatomic) IBOutlet UILabel *aaaaa;


@property (weak, nonatomic) IBOutlet UITextField *name;



@property (weak, nonatomic) IBOutlet UITextField *phone;

@property (weak, nonatomic) IBOutlet UITextField *address;


@property (weak, nonatomic) IBOutlet UITextField *youbian;


@end
#define DeviceHeight            [[UIScreen mainScreen] bounds].size.height
#define DeviceWidth             [[UIScreen mainScreen] bounds].size.width
#define Key_DistrictSelectProvince          @"DistrictSelectProvince"
#define Key_DistrictSelectProvinceCode      @"DistrictSelectProvinceCode"
#define Key_DistrictSelectProvinceSubCode   @"DistrictSelectProvinceSubCode"
#define Key_DistrictSelectProvinceSub       @"DistrictSelectProvinceSub"
#define Key_DistrictSelectCityCode          @"DistrictSelectCityCode"
#define Key_DistrictSelectCity              @"DistrictSelectCity"

@implementation ManagerViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [self.name resignFirstResponder];
    [self.address resignFirstResponder];
    [self.phone resignFirstResponder];
    [self.youbian resignFirstResponder];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0, HIGHT-49, WEITH, 49)];
    lastView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lastView];
    
    self.title = @"新增地址";
    
    UIButton *addbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addbtn.frame = CGRectMake(0, 0, WIDTH, 49);
    addbtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [addbtn setTitle:@"添加收货地址" forState:UIControlStateNormal];
    [addbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addbtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
//    addbtn.backgroundColor = [UIColor greenColor];
    [lastView addSubview:addbtn];
  
}


- (IBAction)pickerButton:(id)sender {
    [self pickerView];
}

- (void)pickerView{
    
    GPDateView * dateView = [[GPDateView alloc] initWithFrame:CGRectMake(0, DeviceHeight-250, DeviceWidth, 250) Data:nil];
    
    [dateView showPickerView];
    
    dateView.ActionDistrictViewSelectBlock = ^(NSString *desStr,NSDictionary *selectDistrictDict){
        self.aaaaa.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvince];
        self.prLL.text = [selectDistrictDict objectForKey:Key_DistrictSelectCity];
        self.areaCC.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvinceSub];
        
    };
    
}



- (void)buttonAction{
    
    if (self.name.text.length == 0 || self.phone.text.length == 0|| self.address.text.length == 0|| self.youbian.text.length == 0 || [self.aaaaa.text isEqualToString:@"省"]) {
        
        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alview.tag = 123306;
        [alview show];
        return;
        
    }
    
    NSDictionary *userData = [ReadLoginData readLoginData];
    
    NSString *userId = [userData objectForKey:@"Number"];
    
    NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd  HH:mm:ss"];
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];
    
    NSDictionary *param = @{
                            @"Id":@"0",
                            @"UserId":userId,
                            @"Province":self.aaaaa.text,
                            @"City":self.prLL.text,
                            @"Countory":self.areaCC.text,
                            @"Address":self.address.text,
                            @"ContactName":self.name.text,
                            @"ContactPhone":self.phone.text,
                            @"ContactMobile":self.phone.text,
                            @"ContactPostCode":self.youbian.text,
                            //                                @"AddTime":[NSString stringWithFormat:@"%@",currentTime],
                            //#warning 添加时间待修改
                            //                                @"AddTime":@"2016-05-03T16:10:06.6102784+08:00",
                            @"AddTime":@"",
                            @"IsDefault":@"0"
                            };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:param AndMenthodName:@"EXPRESSADDRESS" completeBlock:^(id object) {
        NSLog(@"%@",object);
        
        NSInteger responceNumber = [[object objectForKey:@"Id"] integerValue];
        
        if (responceNumber == 0) {
            
            NSString *message = [NSString stringWithFormat:@"%@",[object objectForKey:@"Message"]];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            alert.tag = 10010;
            [alert show];
        } else {
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"填写有误,请检查后重新输入" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            
            alert.tag = 10011;
            
            [alert show];
            
        }
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10010) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}
- (void)alertViewCancel:(UIAlertView *)alertView{
    
    if (alertView.tag == 123303 || alertView.tag == 123305) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }else{
        
    }
  
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self gobackClick];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
